// pages/wuliurongqiguanli/LCIA/gzpd_detail/gzpd_detail.js
import Dialog from '@vant/weapp/dialog/dialog';
const mybehavior = require("../../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/lcia_api",
    popup_key: "",
    value1: "",
    value2: "",
    value22: "",
    value3: "",
    value33: "",
    value4: "",
    value44: "",
    value5: "",
    value55: "",
    popup_show1: false,
    popup_show2: false,
    popup_show3: false,
    popup_show4: false,
    popup_show5: false,
    options: [],
    options1: ["在用", "闲置", "报废"],
    options2: ["未完成", "已完成",],
    options3: ["未完成", "已完成",],
    options4: ["未完成", "已完成",],
    options5: ["未完成", "已完成",],
    choose_img_btn_disabled: false,
    uploader: [],
    note: "",
    row_data: {}

  },
  // 重置
  reset() {
    this.setData({
      value1: "",
      value2: "",
      value22: "",
      value3: "",
      value33: "",
      value4: "",
      value44: "",
      value5: "",
      value55: "",
      uploader: [],
      note: "",
    })
  },
  // 完成盘点
  submit() {
    let arr = this.data.uploader
    let file_id1 = ""
    if (arr.length == 0) {
      wx.showToast({
        title: '请上传一张照片！',
        icon: 'error'
      })
    } else {
      file_id1 = arr[0].file_id
      let url = this.data.baseUrl + "/inventory_manage/finish_inventory "
      let data = {
        tooling_state: this.data.value1,
        location: this.data.value22,
        nameplate: this.data.value33,
        identification: this.data.value44,
        maintenance: this.data.value55,
        attachment: file_id1,
        note: this.data.note,
        item_id: this.data.row_data.id
      }
      console.log(data);
      this.post(url, data).then(res => {
        console.log("提交盘点", res)
        if (res.code == 20000) {

          Dialog.confirm({
            title: "盘点成功",
          })
            .then(() => {
              wx.redirectTo({
                url: '/pages/wuliurongqiguanli/LCIA/gzpd/gzpd',
              })

            })
            .catch(() => {
              // on cancel
            });
        } else {
          wx.showToast({
            title: res.msg + ",请完善带 * 信息！",
            icon: 'none'
          })
        }
      }).catch(err => {
        wx.showToast({
          title: err.msg + ",请完善带 * 信息！",
          icon: 'none'
        })
      })

    }


  },
  // 点击----选取输入框
  onClick1(event) {
    console.log(event.currentTarget.dataset.key);
    // event.detail 为当前输入的值
    let options = this.data['options' + event.currentTarget.dataset.key]
    let popup = 'popup_show' + event.currentTarget.dataset.key
    console.log("123", options);
    this.setData({
      popup_key: event.currentTarget.dataset.key,
      options: options,
      [popup]: true,

    })
  },
  // 选择----输入框关闭
  onClose1() {
    let key = this.data.popup_key
    let popup = 'popup_show' + key
    this.setData({ [popup]: false });
  },
  // 确认---弹出框关闭
  onChange1(event) {
    console.log(event);
    const { picker, value, index } = event.detail;
    let key = this.data.popup_key
    let val = 'value' + key
    let val1 = 'value' + key + key
    let popup = 'popup_show' + key
    this.setData({
      [val]: value,
      [val1]: event.detail.index,
      [popup]: false,
    })
  },

  // 照片选取
  try_upload(e) {
    const self = this;
    self.setData({
      choose_img_btn_disabled: true
    })
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        const tempFilePath = res.tempFiles[0].tempFilePath;
        console.log(tempFilePath);
        wx.showLoading({
          title: '开始上传。。。',
        })

        wx.uploadFile({
          url: "https://logisticsengineering.gree.com/lcia_api/file_manage/fs_upload",
          filePath: tempFilePath,
          name: 'file',
          formData: {
          },
          success(res) {
            const result = JSON.parse(res.data);
            console.log(result)
            wx.hideLoading();
            if (result.code == 20000) {
              const uploader = self.data.uploader;

              const new_uploader = uploader.concat({
                url: "https://logisticsengineering.gree.com/lcia_api/file_manage/fs_download?file_id=" + result.data.file_id,
                isImage: true,
                file_id: result.data.file_id
              })

              self.setData({
                uploader: new_uploader,

              })
              if (self.data.uploader.length < 1) {
                self.setData({
                  choose_img_btn_disabled: false
                })
              }
              wx.showToast({
                title: '上传成功！',
              })
            } else {
              if (self.data.uploader.length < 1) {
                self.setData({
                  choose_img_btn_disabled: false
                })
              }
              wx.showToast({
                title: result.msg,
                icon: "error"
              })
            }

          },
          fail(res) {
            wx.hideLoading();
            if (self.data.uploader.length < 1) {
              self.setData({
                choose_img_btn_disabled: false
              })
            }
            wx.showToast({
              title: "未知错误428",
              icon: "error"
            })
          }
        })

      },
      fail() {
        if (self.data.uploader.length < 1) {
          self.setData({
            choose_img_btn_disabled: false
          })
        }
      }



    })
  },
  handle_delete(e) {
    const index = e.detail.index;
    const file = this.data.uploader[index];
    const file_id = file.url.split("=")[1];
    const uploader = this.data.uploader;
    const self = this;

    let url1 = 'https://logisticsengineering.gree.com/lcia_api/file_manage/fs_delete'
    let data = {
      file_id
    }
    this.post(url1, data).then(res => {
      if (res.code == 20000) {
        wx.showToast({
          title: res.msg,
          icon: 'success'
        })

        uploader.splice(index, 1);
        self.setData({
          choose_img_btn_disabled: false,
        })
        self.setData({
          uploader
        });


      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let self = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      console.log("传过来的数据", data)


      self.setData({
        row_data: data,


      })


    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})